﻿$(function () {
    bindValidate(ajaxSubmit);
    EditDomain();
});

var $tempRadioLang = null;
var $ulDefaultLang = null;
var $radDefaultLang = null;
var objLangList = null;

//选择语言
//langId 语言Id
//nameHK 语言名称
//chkLang 当前语言多选框
function selectLang(langId, name, chkLang) {
    var objLangList = [{ "LangId": langId, "Name": name }];
    if ($tempRadioLang == null || $ulDefaultLang == null) {
        $tempRadioLang = $("#tempRadioLang");
        $ulDefaultLang = $("#ulDefaultLang");
    }

    if (chkLang.checked == true) {
        $(chkLang).parent().css("color", "red");//設置選中的為紅色
        $tempRadioLang.tmpl(objLangList).appendTo($ulDefaultLang); //添加模塊列表
    }
    else {
        $(chkLang).parent().css("color", "");//去掉紅色
        $radDefaultLang = $("#radDefaultLang" + langId, $ulDefaultLang);
        $radDefaultLang.parent().parent().remove();
    }
}

function selectedChk(strlangIds) {
    var $chks = $("[type='checkbox']", "#ulLangList");
    if ($chks.length > 0) {
        var langIds = strlangIds.split(',');
        for (var i = 0; i < langIds.length; i++) {
            if (langIds[i] !== "") {
                var chk = $("#ulLangList").find('[value="' + langIds[i] + '"]');
                if (chk != undefined && chk.length > 0) {
                    chk[0].checked = false;
                    chk.click();
                }
            }
        }
    }
}

function selectedRadio(langId) {
    var $radio = $("[type='checkbox']", "#ulLangList");
    if ($radio.length > 0) {
        var radio = $('#ulDefaultLang').find('[value="' + langId + '"]');
        if (radio.length > 0) {
            radio.click();
        }
    }
}

//Ajax提交
//currObj 當前點擊對象
function ajaxSubmit() {
    var $form = $("#form1");

    var data = $form.serialize();
    var url = "Handler.ashx?Operate=AddOrEdit&siteId=" + siteId;

    msgBox.showMsgWait(txt_Saving);
    var ajax = $.post(url, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            msgBox.hidBox()
            msgBox.showMsgWait(txt_Redirecting)
            if (flag) {
                if (newUrl != undefined && newUrl != "" && newUrl != null) {
                    location.href = newUrl;
                    return;
                }
            }
            location.href = ajaxMsg.url;
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
        return false;
    }, "json");
    ajax.error = function () {
        msgBox.showMsgErr(txt_Error);
    }
    return false;
}

function registerDomain() {
    var result = confirm(txt_RegisDomainConfirm);
    if (result) {
        flag = true;
        ajaxSubmit();
    }
}

$(document).ready(function () {
    window.ymHeight = $(window).height() * 0.9;
    window.ymWidth = $(window).width() * 0.9;
});

function chooseDomain() {
    var data2 = { title: txt_DomainManager, width: ymWidth, height: ymHeight, handler: closeDomainManger, iframe: { src: '../domain/list.aspx?siteId=' + siteId } };
    data2 = $.extend(ymdata, data2);
    ymPrompt.win(data2);
}

function closeDomainManger() {
    var url = 'handler.ashx?operate=GetDomainList';
    var data = { siteId: siteId };
    $.get(url, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            if (ajaxMsg.msg != undefined && ajaxMsg.msg != '') msgBox.showMsgOk(ajaxMsg.msg);
            var html = $.templates($('#domainListTemplate').text()).render(ajaxMsg.data);
            $('#DomainDiv').html(html)
        } else {
            msgBox.showMsgErr(ajaxMsg.msg);
        }
    });
}

function EditDomain() {
    $(document).on('click', '[name="chkdomain"]', function () {
        var url = "Handler.ashx?operate=EditDomain";
        if (!$(this).prop('checked')) {
            var flag = confirm(txt_SureDeleteDomain);
            if (!flag) {
                $(this).prop('checked', true);
                return;
            }
        }

        var domainId = $(this).val();
        if (domainId == 0) {
            msgBox.showMsgErr(txt_Error);
            return;
        }
        var domainName = $(this).parent().text().trim();
        var obj = { DomainId: domainId, Domain: domainName };
        var data = { siteId: siteId, domainId: domainId };
        msgBox.showMsgWait(txt_TryingToModify);
        var ajax = $.post(url, data, function (ajaxMsg) {
            if (ajaxMsg.status == 1) {
                msgBox.showMsgOk(ajaxMsg.msg);

                var lineDomain = $('[name="deldomain"]').find('[domainId="' + domainId + '"]');
                if (lineDomain.length > 0) {
                    $(lineDomain).parents('li').remove();
                } else {
                    var html = $.templates($('#lineDomainTemplate').text()).render(obj);
                    $(html).appendTo($('[name="deldomain"]'));
                }
            } else {
                $(this)[0].checked = !$(this)[0].checked;
            }
        });
        ajax.error = function () {
            msgBox.showMsgErr(txt_Error);
        }
    });

    $(document).on('click', 'span[domainid]', function () {//删除
        var flag = confirm(txt_SureDeleteDomain);
        if (!flag) {
            return;
        }

        var url = "Handler.ashx?operate=EditDomain";
        var domainId = $(this).attr('domainId');
        if (domainId == 0) {
            msgBox.showMsgErr(txt_Error);
            return;
        }

        var data = { siteId: siteId, domainId: domainId };
        msgBox.showMsgWait(txt_TryingToDelete);
        var ajax = $.post(url, data, function (ajaxMsg) {
            if (ajaxMsg.status == 1) {
                $('span[domainId="' + domainId + '"]').parents('li').remove(); //删除点击的上线域名
                if ($('input[domainId="' + domainId + '"]').length > 0) {
                    $('input[domainId="' + domainId + '"]')[0].checked = !$('input[domainId="' + domainId + '"]')[0].checked;
                }
            }
            msgBox.showMsgOk(ajaxMsg.msg)
        });
        ajax.error = function () {
            msgBox.showMsgErr(txt_Error);
        }
    });
}